const {
  $util,
} = getApp();
Page({
  data: {
    tabbarList: [{
      text: '详情',
    },
    {
      text: '提交记录',
    },
    ],
    page: 1,
    hasMore: true,
    loaded: false,
    rtIdx: 0,
  },
  onLoad(opts) {
    this.setData({
      id: opts.id,
      idx: opts.idx,
    }, () => {
      this.loadDeatail(opts.id);
      this.getHistoryList();
      wx.setNavigationBarTitle({
        title: opts.barTitle && decodeURIComponent(opts.barTitle) || '志愿服务详情',
      });
    });
  },
  onShow() {
    this.initHistoryList();
  },
  // 加载详情
  loadDeatail(id) {
    $util.request({
      url: 'entry/wxapp/partyman.home.getservicedetails',
      data: {
        id,
      },
      success: (res) => {
        this.setData({
          detail: res.data,
          isLoaded: true,
          record: res.record || {},
        });
      },
    });
  },
  switchTab(e) {
    this.setData({
      rtIdx: e.detail.idx,
    });
  },
  // 初始化历史记录列表
  initHistoryList() {
    this.setData({
      page: 1,
    }, () => {
      this.getHistoryList();
    });
  },
  // 获取历史记录列表
  getHistoryList() {
    const D = this.data;
    $util.request({
      url: 'entry/wxapp/partyman.home.getrecordlist',
      data: {
        id: D.id,
        page: D.page,
      },
      success: (res) => {
        let list = res.data;
        if (D.page > 1) {
          list = [...D.list, ...list];
        }
        this.setData({
          list,
          hasMore: list.length < parseInt(res.total, 0),
          page: list.length < parseInt(res.total, 0) ? D.page + 1 : D.page,
          loaded: true,
        });
      },
    });
  },
  // 加载更多
  loadMore() {
    this.data.hasMore && this.getHistoryList();
  },
  swiperChange(e) {
    this.setData({
      rtIdx: e.detail.current,
    });
  },
});